package com.example.administrator.stulist;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class EdtStudent extends AppCompatActivity {
    private EditText edtName;
    private EditText adtAge;
    private RadioButton radioButton1;
    private RadioButton radioButton2;
    private RadioButton radioButton3;
    private RadioButton radioButton4;
    private RadioButton radioButton5;
    private int i;
    Button btn_save;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edt_student);
        edtName=findViewById(R.id.name_add);
        adtAge=findViewById(R.id.age_add);
        radioButton1=findViewById(R.id.radioButton1);
        radioButton2=findViewById(R.id.radioButton2);
        radioButton3=findViewById(R.id.radioButton3);
        radioButton4=findViewById(R.id.radioButton4);
        radioButton5=findViewById(R.id.radioButton5);
        btn_save=findViewById(R.id.finish);
        final Intent intent=getIntent();
        i=intent.getIntExtra("student",1);
        String name=intent.getStringExtra("name");
        String  age=String.valueOf(intent.getIntExtra("age",0));
        int imgid=intent.getIntExtra("imgid",0);
        edtName.setText(name);
        adtAge.setText(age);
        if(imgid==R.drawable.dog1){
            radioButton1.setChecked(true);
        }
        if(imgid==R.drawable.dog2){
            radioButton2.setChecked(true);
        }
        if(imgid==R.drawable.dog3){
            radioButton3.setChecked(true);
        }
        if(imgid==R.drawable.dog4){
            radioButton4.setChecked(true);
        }
        if(imgid==R.drawable.dog5){
            radioButton5.setChecked(true);
        }

        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1=new Intent();
                String name=edtName.getText().toString();
                int age=Integer.parseInt(adtAge.getText().toString());
                int imgid=R.drawable.dog1;
                if(radioButton1.isChecked()){
                    imgid=R.drawable.dog1;
                }
                if(radioButton2.isChecked()){
                    imgid=R.drawable.dog2;
                }
                if(radioButton3.isChecked()){
                    imgid=R.drawable.dog3;
                }
                if(radioButton4.isChecked()){
                    imgid=R.drawable.dog4;
                }
                if(radioButton5.isChecked()){
                    imgid=R.drawable.dog5;
                }
                intent1.putExtra("student",i);
                intent1.putExtra("name",name);
                intent1.putExtra("age",age);
                intent1.putExtra("imgid",imgid);
                setResult(2001,intent1);
                finish();
            }
        });

    }
}
